package network;


import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.Vector;
import jogo.Mapa;

public class ThreadClientMap extends Thread{

		private Mapa mapa;
		private Vector<ObjectOutputStream> outs;

		public ThreadClientMap(Mapa mapa) {
			this.mapa = mapa;
			this.outs = new Vector<ObjectOutputStream>();
		}

		public synchronized Vector<ObjectOutputStream> getClientOuts(){
			return outs;
		}
		
		public void run() {
			try {
				while(true){
					broadcast();
					sleep(100);
				}
			} catch (InterruptedException e) {
			}
		}
		
		private synchronized void broadcast() {
			for(ObjectOutputStream out: getClientOuts()){
				try {			
					mapa.repaint();
					out.reset();
					out.writeObject(mapa);		
				} catch (IOException e) {
				}
			}
		}

		public synchronized void adicionaBuffer(ObjectOutputStream outs) {
			getClientOuts().add(outs);
		}

		public synchronized void removeOutputBuffer(ObjectOutputStream out) {
			outs.remove(out);
		}
	}